<?php

namespace App\Jobs\Attachment;
// 加载采集环境

use App\Models\WxAttachment;
use App\Wen\Utils\ApiUtils;
use App\Wen\Utils\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Qcloud\Cos\Client;

class VideoCosReviewSubmit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $attachment_id;
    protected $cosClient = null;
    private $Bucket = null;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($attachment_id)
    {
        $this->attachment_id = $attachment_id;
    }

    private function initConfig(){
        if($this->cosClient === null){
            $cosConfig = Settings::get('cosv5', []);
            $this->cosClient = new Client(array(
                'region' => $cosConfig['region'],
                'scheme' => 'https',
                'credentials' => array(
                    'secretId' => $cosConfig['secretId'],
                    'secretKey' => $cosConfig['secretKey'],
                )
            ));
            $this->Bucket = $cosConfig['bucket'] . '-' . $cosConfig['appId'];
        }
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->initConfig();
        if(_empty_($this->attachment_id)){
            $attachment = WxAttachment::where('cos_review', 4)->whereIn('type', ['mp4'])->first();
            if($attachment){
                ApiUtils::cos_review_submit($this->cosClient, $this->Bucket, $attachment);
            }
        }else{
            $attachment = WxAttachment::where('cos_review', 4)->whereIn('type', ['mp4'])->where('id', $this->attachment_id)->first();
            if($attachment){
                ApiUtils::cos_review_submit($this->cosClient, $this->Bucket, $attachment);
            }
        }
    }
}
